package com.xiaoshuidi.cloud.module.member.controller.app.index;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.module.member.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.member.enums.AppIndexPageItemEnum;
import com.xiaoshuidi.cloud.module.member.service.index.AppIndexPageConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;

@Tag(name = "租客APP - 首页接口")
@RestController
@RequestMapping(ApiConstants.NAME + "/app-config")
@Validated
@Slf4j
@RefreshScope
public class AppIndexPageController {
    @Resource
    AppIndexPageConfigService appIndexPageConfigService;
    @Value("${home_show_config}")
    private String homeShowConfig;
    @Value("${app_version_update_config}")
    private String appVersionUpdateConfig;

    @PermitAll
    @GetMapping("/index-config/{id}")
    @Parameters({
            @Parameter(name = "lat", description = "纬度"),
            @Parameter(name = "lon", description = "经度"),
            @Parameter(name = "apartmentId", description = "门店ID"),
    })
    @Operation(summary = "APP首页配置-获取配置")
    public CommonResult<Object> getIndexConfig(@PathVariable Integer id, String lat, String lon, Long apartmentId) {
        AppIndexPageItemEnum item = AppIndexPageItemEnum.getEnumByIndex(id);
        if (item != null) {
            Object vo = appIndexPageConfigService.getAppIndexConfig(item, lat, lon, apartmentId);
            return CommonResult.success(vo);
        } else {
            return CommonResult.success();
        }
    }

    @Operation(summary = "首页推荐门店、酒店、猜你喜欢展不展示")
    @PermitAll
    @GetMapping("/home-show")
    public CommonResult<Boolean> homeShow(String version) {
        if (StrUtil.isEmpty(homeShowConfig)) {
            return CommonResult.success(true);
        }
        JsonNode jsonNode = JsonUtils.parseTree(homeShowConfig);
        JsonNode value = jsonNode.get(version);
        if (value == null) {
            return CommonResult.success(true);
        }
        boolean flag = value.asBoolean();
        return CommonResult.success(flag);
    }

    @Operation(summary = "租客端APP是否强制更新")
    @PermitAll
    @GetMapping("/app-version-renew")
    public CommonResult<String> appVersionRenew() {
        return CommonResult.success(appVersionUpdateConfig);
    }

}
